﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.EntityClient;
using System.Configuration;

namespace SLServiceLibrary
{
    public class Server
    {
        private static string providerName = "System.Data.SqlClient";
        private string dbUrl;

        public string DbUrl
        {
            get { return dbUrl; }
            set { dbUrl = value; }
        }
        private string dbUsername;

        public string DbUsername
        {
            get { return dbUsername; }
            set { dbUsername = value; }
        }
        private string dbPassword;

        public string DbPassword
        {
            get { return dbPassword; }
            set { dbPassword = value; }
        }
        private string dbName;

        public string DbName
        {
            get { return dbName; }
            set { dbName = value; }
        }

        public bool IsNull()
        {
            return (String.IsNullOrWhiteSpace(dbUrl) &&
                String.IsNullOrWhiteSpace(dbUsername) &&
                String.IsNullOrWhiteSpace(dbPassword) &&
                String.IsNullOrWhiteSpace(dbName));
        }

        public string GetConnectString()
        {
            //读取App.config
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            try
            {
                this.DbUsername = config.AppSettings.Settings["dbusername"].Value;
                this.DbPassword = config.AppSettings.Settings["dbpassword"].Value;
                this.DbUrl = config.AppSettings.Settings["dbserver"].Value;
                this.DbName = config.AppSettings.Settings["dbname"].Value;
            }
            catch (Exception)
            {
                
                throw;
            }
            // Initialize the connection string builder for the
            // underlying provider.
            SqlConnectionStringBuilder sqlBuilder =
                new SqlConnectionStringBuilder();

            // Set the properties for the data source.
            sqlBuilder.DataSource = DbUrl;
            sqlBuilder.InitialCatalog = DbName;
            sqlBuilder.IntegratedSecurity = false;
            sqlBuilder.UserID = DbUsername;
            sqlBuilder.Password = DbPassword;

            // Build the SqlConnection connection string.
            string providerString = sqlBuilder.ToString();

            // Initialize the EntityConnectionStringBuilder.
            EntityConnectionStringBuilder entityBuilder =
                new EntityConnectionStringBuilder();

            //Set the provider name.
            entityBuilder.Provider = providerName;

            // Set the provider-specific connection string.
            entityBuilder.ProviderConnectionString = providerString;

            entityBuilder.Provider = providerName;

            // Set the Metadata location.
            entityBuilder.Metadata = "res://*/GISModel.csdl|res://*/GISModel.ssdl|res://*/GISModel.msl";
            //"res://*"; //或从.config文件中copy
            //@"res://*/NBModel.csdl|
            //res://*/NBModel.ssdl|
            //res://*/NBModel.msl";
            return entityBuilder.ToString();
        }
    }
}
